package com.dizang.ddd.business.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.dizang.ddd.dto.ProjectNodeDTO;
import com.dizang.ddd.entity.AbstractProjectBuilder;

/**
 * 
 * @author kelvin.cai
 *
 */
public class CreateProjectByJsonBusinessImpl extends AbstractCreateProjectBusiness {

	private static final Logger logger = LoggerFactory.getLogger(AbstractProjectBuilder.class);

	public boolean createProject(String path, String file) {
		JSONObject jsonFile = getJSONFile(file);
		ProjectNodeDTO javaObject = jsonFile.toJavaObject(ProjectNodeDTO.class);
		builEachProject(path, null, javaObject);
		return true;
	}

	public List<String> buildPackage(ProjectNodeDTO node) {
		List<ProjectNodeDTO> subNode2 = node.getSubNode();
		List<String> list = new ArrayList<>();
		for (ProjectNodeDTO projectNode : subNode2) {

			List<ProjectNodeDTO> subNode = projectNode.getSubNode();
			if (subNode != null && subNode.size() > 0) {
				List<String> buildPackage = buildPackage(projectNode);
				if (buildPackage != null && buildPackage.size() > 0) {
					for (String string : buildPackage) {
						StringBuilder stringBuilder = new StringBuilder();
						String name = projectNode.getName();
						stringBuilder.append(name);
						stringBuilder.append("/");
						stringBuilder.append(string);
						String string2 = stringBuilder.toString();
						list.add(string2);
					}
				}
			} else {
				StringBuilder stringBuilder = new StringBuilder();
				String name = projectNode.getName();
				stringBuilder.append(name);
				stringBuilder.append("/");
				String string2 = stringBuilder.toString();
				list.add(string2);
			}
		}
		return list;

	}

	private JSONObject getJSONFile(String file) {

		JSONObject jsonArray = null;
		BufferedReader reader = null;
		StringBuilder jsonStrs = new StringBuilder();
		InputStream inputStream = null;
		try {
			URL resource = CreateProjectByJsonBusinessImpl.class.getClassLoader().getResource("");
			String path = resource.getPath();
			inputStream = new FileInputStream(new File(path + file));
			InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
			reader = new BufferedReader(inputStreamReader);
			String tempStr = null;
			while ((tempStr = reader.readLine()) != null) {
				jsonStrs.append(tempStr);
			}
			reader.close();
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
				}
			}
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					logger.error(e.getMessage(), e);
				}
			}
		}
		logger.info(jsonStrs.toString());
		try {
			jsonArray = JSONObject.parseObject(jsonStrs.toString().trim());
		} catch (IllegalStateException ex) {
			logger.error("JSON  File is wrong");
		} catch (JSONException ex) {
			logger.error("JSON  File is wrong");
		}
		return jsonArray;
	}


}
